package com.nand.blockdemo.ui;

import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.nand.blockdemo.R;
import com.nand.blockdemo.adapter.RcvGridTitleAdapter;
import com.nand.blockdemo.bean.GridItem;
import com.nand.blockdemo.bean.ModuleBean;
import com.nand.blockdemo.bean.ModuleItemBean;
import com.nand.blockdemo.bean.ModuleListBean;
import com.nand.blockdemo.constant.URL;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.module.function.bean.ManageBean;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class FunctionActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private List<GridItem> dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_function;
    }

    @Override
    protected void initView() {
        mActionBar().setCenterText("协同应用");

        recyclerView = findViewById(R.id.recycler_view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(gridLayoutManager);

    }

    @Override
    protected boolean isActionBar() {
        return true;
    }

    @Override
    protected void initData() {
//        dataList = new ArrayList<>();
//        for (int i = 0; i < 20; i++) {
//            GridItem gridItem = new GridItem();
//            gridItem.setText("功能名称");
//
//            int num = (int) (Math.random() * 5 + 1);
//            switch (num) {
//                case 1:
//                    gridItem.setDrawableId(R.mipmap.home_equipment_inspect_icon);
//                    break;
//                case 2:
//                    gridItem.setDrawableId(R.mipmap.home_equipment_report_icon);
//                    break;
//                case 3:
//                    gridItem.setDrawableId(R.mipmap.home_equipment_ledger_icon);
//                    break;
//                case 4:
//                    gridItem.setDrawableId(R.mipmap.home_equipment_maintain_icon);
//                    break;
//                default:
//                    gridItem.setDrawableId(R.mipmap.home_comprehensive_query_icon);
//            }
//
//            dataList.add(gridItem);
//        }
//
//
//        //position: 插入item的位置，注意这里的下标是包含title的（title算一个item，并且所有item随着插入的title的增多而改变），
//        //即该position参数可以理解为包含title的所有item中title所处于的插入的位置
//        adapter.addTitle(0, "设备模块");
//        adapter.addTitle(4, "生产模块");
//        adapter.addTitle(9, "质量模块");
//        adapter.addTitle(13, "计划模块");
//        adapter.addTitle(17, "仓储模块");
//
//        recyclerView.setAdapter(adapter);


        OkGoUtils.getInstance().post(this, URL.MODULE_POWER_URL, null, 2, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                if (response.getCode() == 200) {
                    String data = response.getDataInfo().toString();
                    List<ModuleBean> mList = JSON.parseArray(data, ModuleBean.class);
                    List<ModuleBean.ChildBean> mAllItem = new ArrayList<>();
                    RcvGridTitleAdapter adapter = new RcvGridTitleAdapter(FunctionActivity.this);
                    int index = 0;
                    for (int i = 0; i < mList.size(); i++) {
                        adapter.addTitle(index, mList.get(i).getModuleName());
                        List<ModuleBean.ChildBean> mItems = mList.get(i).getChild();
                        int size = mItems.size();
                        index += (size + 1);
                        mAllItem.addAll(mItems);
                    }
                    adapter.addData(mAllItem);
                    recyclerView.setAdapter(adapter);

                } else {
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onError(Throwable e, int id) {
                Log.e("TAG", "id:" + id + "-----------" + e.getMessage());
            }
        });
    }


}
